{-# OPTIONS -Wall #-}


module Demo01 where

import ZMidi.Basic.Kernel 
import ZMidi.Basic.Symbolic

import qualified ZMidi.Basic.Primitive.EventList as Prim

import ZMidi.Core                               -- package: zmidi-core

import Data.Monoid
import System.Directory

import Prelude hiding ( reverse ) 

main :: IO ()
main = demo01 >> demo02

-- default tempo is 120 beats per minute

demo01_midi :: FilePath
demo01_midi = "./out/new_demo01.mid"

demo01 :: IO ()
demo01 = do
    createDirectoryIfMissing True "./out/"
    putStrLn "Writing out/new_demo01.mid..."
    writeMidi demo01_midi sections
  where
    sections = trace standardContext $ phrase1 >< phrase2 >< reverse phrase2
    phrase1  = phrase $ event1 0 $ sym1 $ symEvent (instrument honky_tonk)
                      >< note C 4 dquarter 
                      >< note C 5 dquarter 
    phrase2  = phrase $ event1 0 $ sym1 $ note E 4 deighth >< note G 4 deighth



demo02 :: IO ()
demo02 = do
    ans <- readMidi demo01_midi
    case ans of
      Left (n,msg) -> putStrLn $ "Parse failure at " ++ show n ++ ": " ++ msg
      Right m      -> printMidi m


es1 :: Prim.EventList
es1 = Prim.eventList $ mconcat
    [ Prim.onoff 0.0 (on 0x3c) 0.5 (off 0x3c)
    , Prim.onoff 0.5 (on 0x48) 0.5 (off 0x48)
    ]
  where
    on  pch = VoiceEvent $ NoteOn 00 pch 127
    off pch = VoiceEvent $ NoteOff 00 pch 127


es2 :: Prim.EventList
es2 = Prim.reverse es1
